<?php
	require_once(dirname(__FILE__) . '/viae/createImageFile.php');
	
	try {
		$uploadDir = dirname(__FILE__) . '/uploads/';
		$scale = $_POST["scale"];
		$height = $_POST["height"] * $scale;
		$width = $_POST["width"] * $scale;
		$dpi_x = $_POST["dpi_x"];
		$dpi_y = $_POST["dpi_y"];
		$img = $_POST['imgBase64'];
		$containerWidthInch = $_POST["containerWidthInch"];
		$containerHeightInch = $_POST["containerHeightInch"];
		$containerWidthPx = $containerWidthInch * $dpi_x;
		$containerHeightPx = $containerHeightInch * $dpi_y;
	/*
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, "debug create_image_file.php\n");
fwrite($ourFileHandle, "$uploadDir\n");
fwrite($ourFileHandle, "$scale\n");
fwrite($ourFileHandle, "$height\n");
fwrite($ourFileHandle, "$width\n");
fwrite($ourFileHandle, "$dpi_x\n");
fwrite($ourFileHandle, "$dpi_y\n");
fwrite($ourFileHandle, "$containerWidthInch\n");
fwrite($ourFileHandle, "$containerHeightInch\n");
fwrite($ourFileHandle, "$containerWidthPx\n");
fwrite($ourFileHandle, "$containerHeightPx\n");
fwrite($ourFileHandle, "$img\n");
fclose($ourFileHandle);*/

		$matches = array();
		preg_match('#data:image/([a-zA-Z]+);base64#', $img, $matches);
		$type = $matches[1];
		
		$pdf = createImageFile($uploadDir, $containerWidthPx, $containerHeightPx, $scale, $img, $type, $dpi_x, $dpi_y);
		$pdf->Output($uploadDir . uniqid() . ".pdf");
	} catch (Exception $e){
		echo "<b><u>VIAE_ERROR</u></b><br/>" . $e->getMessage(); 
	}
	function curPageURL() {
 return basename($_SERVER['REQUEST_URI']);
}
?>